<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: page.php 130 2012-01-14 13:29:27Z info@sah-company.com $
 */

/**
 * Load dependencies.
 */
Registry::instance()->loader->model(array(
	'page_localized',
	'template'
	)
);

/**
 * Kalaisoo Page Model Class.
 *
 * @package Kalaisoo
 * @subpackage Model
 * @author $Author: info@sah-company.com $
 */
class Model_Page extends Model
{
	/**
	 * __construct.
	 */
	public function construct()
	{
		parent::__construct();
	}
	
	/**
	 * returns the tags of a bean.
	 *
	 * @return array
	 */
	public function getTags() {
		return array(
			$this->bean->name
		);
	}
	
	/**
	 * returns an array with special actions the model can take.
	 *
	 * @return array
	 */
	public function actions()
	{
		if ( ! $this->bean->getId()) return array();
		return array(
			'preview' => true
		);
	}
	
	/**
	 * redirects to this pages url.
	 */
	public function preview()
	{
		$this->controller()->redirect(ahref(sprintf('%s/%s', Registry::instance()->language, $this->bean->url)));
	}
	
	/**
	 * loads the array $this->gestalt of this bean for use with Model::gestalt().
	 */
	public function loadGestalten()
	{
		$this->gestalt['default'] = array(
			array(
				'type' => 'text',
				'name' => 'name',
				'help' => __('page_name_help'),
				'parameters' => array(
					'placeholder' => __('page_name_placeholder'),
					'required' => 'required'
				)
			),
			array(
				'type' => 'text',
				'name' => 'url',
				'help' => __('page_url_help'),
				'parameters' => array(
					'placeholder' => __('page_url_placeholder'),
					'required' => 'required'
				)
			),
			array(
				'type' => 'nested',
				'name' => 'template',
				'parameters' => array(
					'select' => 'name'
				)
			),
			array(
				'type' => 'checkbox',
				'name' => 'cacheable',
				'help' => __('page_cacheable_help'),
				'parameters' => array(
					'value' => 1
				)
			),
			array(
				'type' => 'password',
				'name' => 'pw',
				'help' => __('page_pw_help')
			),
			array(
				'type' => 'checkbox',
				'name' => 'pw_helper',
				'help' => __('page_pw_helper_help'),
				'parameters' => array(
					'value' => 1
				)
			),
			array(
				'type' => 'own',
				'name' => 'page_localized'
			)
		);
	}
	
	/**
	 * validates the data and returns either true or false.
	 *
	 * @return bool
	 */
	public function validate()
	{
		$valid = true;
		return $valid;
	}
	
	/**
	 * open.
	 */
	public function open()
	{
	}

	/**
	 * dispense.
	 */
	public function dispense()
	{
		$this->bean->setMeta('buildcommand.unique', array(array('url')));
	}

	/**
	 * update.
	 */
	public function update()
	{
		if ($this->bean->pw_helper) {
			$this->bean->pw = sha1($this->bean->pw);
		}
		if ( ! $this->bean->cacheable) $this->bean->cacheable = false;
	}
	
	/**
	 * after_update.
	 */
	public function after_update()
	{
	}
	
	/**
	 * delete.
	 */
	public function delete()
	{
	}
	
	/**
	 * after_delete.
	 */
	public function after_delete()
	{
	}
}
?>